package org.sith.gl.taskmanager.client.tasks;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.datepicker.client.DatePicker;
import org.sith.gl.taskmanager.client.TasksRemoteService;
import org.sith.gl.taskmanager.client.beans.TaskEntityGWT;
import org.sith.gl.taskmanager.client.beans.UserEntityGWT;

import java.util.Date;
import java.util.List;


class SaveButtonClickHandler implements ClickHandler {
    private final UserEntityGWT addedBy;
    private final ListBox usersList;
    private final List<UserEntityGWT> users;
    private final TextBox desc;
    private final DatePicker creationDatePicker;
    private final DatePicker dueDatePicker;
    private final PopupPanel popupPanel;

    public SaveButtonClickHandler(FormWidgets formWidgets) {
        this.addedBy = formWidgets.getAddedBy();
        this.usersList = formWidgets.getUsersList();
        this.users = formWidgets.getUsers();
        this.desc = formWidgets.getDesc();
        this.creationDatePicker = formWidgets.getCreationDatePicker();
        this.dueDatePicker = formWidgets.getDueDatePicker();
        this.popupPanel = formWidgets.getPopupPanel();
    }

    @Override
    public void onClick(ClickEvent clickEvent) {
        TaskEntityGWT newTask = new TaskEntityGWT();
        newTask.setAddedBy(addedBy);
        int index = usersList.getSelectedIndex();
        if (index == 0) {
            Window.alert("Choose user");
            return;
        }
        String login = usersList.getItemText(index);
        UserEntityGWT to = getUserByLogin(users, login);
        if (to == null) {
            Window.alert("User not found");
            return;
        }

        newTask.setAssignedTo(to);
        String descText = desc.getText();
        if (descText == null || desc.getText().isEmpty()) {
            Window.alert("Please type description");
            return;
        }
        newTask.setDescription(descText);

        Date creationDate = creationDatePicker.getHighlightedDate();
        Date dueDate = dueDatePicker.getHighlightedDate();

        if (dueDate == null) {
            Window.alert("Please choose due date");
            return;
        }
        if (creationDate == null) {
            Window.alert("Please choose creation date");
            return;
        }
        if (creationDate.getTime() > dueDate.getTime()) {
            Window.alert("Due day must be after Creation date");
            return;
        }


        newTask.setCreatedDate(AddTaskPopup.dateTimeFormat.format(creationDate));
        newTask.setDueDate(AddTaskPopup.dateTimeFormat.format(dueDate));

        newTask.setComplete(false);

        TasksRemoteService.App.getInstance().saveOrUpdateTask(newTask, new AsyncCallback<Void>() {
            @Override
            public void onFailure(Throwable throwable) {
                popupPanel.hide();
                Window.alert("Error during saving task");
            }

            @Override
            public void onSuccess(Void aVoid) {
                popupPanel.hide();
            }
        });


    }

    private UserEntityGWT getUserByLogin(List<UserEntityGWT> users, String login) {
        for (UserEntityGWT user : users) {
            if (user.getLogin().equals(login)) {
                return user;
            }
        }
        return null;
    }

}
